package com.software.driver.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.software.common.CommonUtils;
import com.software.driver.dao.Driver;
import com.software.driver.dao.DriverTransaction;
import com.software.form.taskform.dao.TaskForm;
import com.software.form.taskform.dao.TaskFormTransaction;

public class QueryTaskServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String action = request.getParameter("action");
		String name = (String) request.getSession().getAttribute("drivername");
		TaskFormTransaction tft = (TaskFormTransaction) CommonUtils.getBean("taskFormTransaction");
		if(action.equals("current")){
			TaskForm tf = tft.queryNow(name);
			request.getSession(true).setAttribute("current", tf.getId());
			JSONObject json = new JSONObject();
			try {
				json.put("no",tf.getNo());
				json.put("starttime",tf.getStartTime());
				json.put("duetime",tf.getDueTime());
				json.put("drivername",tf.getDriverName());
				json.put("comment",tf.getComment());
				CommonUtils.outputJsonData(json.toString(), response);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if(action.equals("history")){
			List<TaskForm> list = tft.queryByName(name);
			JSONObject json = new JSONObject();
			List tasks = new ArrayList();
			for(int i = 0; i < list.size(); i++){
				JSONArray task = new JSONArray();
				if(list.get(i).getEndTime() == null)
					continue;
				task.put(list.get(i).getNo());
				task.put(list.get(i).getStartTime());
				task.put(list.get(i).getDueTime());
				task.put(list.get(i).getEndTime());
				task.put(list.get(i).getDriverName());
				task.put(list.get(i).getComment());
				tasks.add(task);
			}
			try {
				json.put("tasks", tasks);
				CommonUtils.outputJsonData(json.toString(), response);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if(action.equals("status")){
			int id = (Integer)request.getSession(true).getAttribute("current");
			TaskForm tf = tft.getTaskFormById(id);
			String status = tf.getStatus();
			if(status != null){
				String[] results = status.split("/");
				String str = "";
				for(int i = 0; i < results.length; i++)
					str += results[i]+"<br />";
				System.out.println(str);
				JSONObject json = new JSONObject();
				try {
					json.put("status", str);
					CommonUtils.outputJsonData(json.toString(), response);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}else if(action.equals("name")){
			JSONObject o = CommonUtils.readJSONStringFromRequestBody(request, response);
			try {
				String n = o.getString("name");
				TaskForm tf = (TaskForm) tft.queryByName(n).get(0);
				if(tf != null){
					List tasks = new ArrayList();
					JSONObject json = new JSONObject();
					JSONArray task = new JSONArray();
					task.put(tf.getNo());
					task.put(tf.getDriverName());
					task.put(tf.getStartTime());
					task.put(tf.getDueTime());
					if(tf.getEndTime() == null)
						task.put("-------");
					else 
						task.put(tf.getEndTime());
					task.put(tf.getComment());
					if(tf.getStatus() == null)
						task.put("-------");
					else
						task.put(tf.getStatus());
					tasks.add(task);
					json.put("tasks", tasks);
					CommonUtils.outputJsonData(json.toString(), response);
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if(action.equals("no")){
			JSONObject o = CommonUtils.readJSONStringFromRequestBody(request, response);
			try {
				String n = o.getString("no");
				TaskForm tf = (TaskForm) tft.queryByNo(n).get(0);
				if(tf != null){
					List tasks = new ArrayList();
					JSONObject json = new JSONObject();
					JSONArray task = new JSONArray();
					task.put(tf.getNo());
					task.put(tf.getDriverName());
					task.put(tf.getStartTime());
					task.put(tf.getDueTime());
					if(tf.getEndTime() == null)
						task.put("-------");
					else 
						task.put(tf.getEndTime());
					task.put(tf.getComment());
					if(tf.getStatus() == null)
						task.put("-------");
					else
						task.put(tf.getStatus());
					tasks.add(task);
					json.put("tasks", tasks);
					CommonUtils.outputJsonData(json.toString(), response);
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if(action.equals("all")){
			String motoEmail = (String) request.getSession().getAttribute("user");
			DriverTransaction dt = (DriverTransaction)CommonUtils.getBean("driverTransaction");
			List<Driver> drivers = dt.getDriversByMoto(motoEmail);
			if(drivers != null){
				try {
					List tasks = new ArrayList();
					JSONObject json = new JSONObject();
					for(int i = 0; i < drivers.size(); i++){
							
							String driverName = drivers.get(i).getName();
							List<TaskForm> ts = tft.queryByName(driverName);
							if(ts!=null){
								TaskForm tf = (TaskForm)ts.get(0);
								if(tf != null){
									JSONArray task = new JSONArray();
									task.put(tf.getNo());
									task.put(tf.getDriverName());
									task.put(tf.getStartTime());
									task.put(tf.getDueTime());
									if(tf.getEndTime() == null)
										task.put("-------");
									else 
										task.put(tf.getEndTime());
									task.put(tf.getComment());
									if(tf.getStatus() == null)
										task.put("-------");
									else
										task.put(tf.getStatus());
									tasks.add(task);
								}
							}
					}
					json.put("tasks", tasks);
					CommonUtils.outputJsonData(json.toString(), response);
			 } catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(req, resp);
	}
}
